package com.whfc.common.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class RandomUtil {

    private static char[] chars = new char[]{
            'A', 'B', 'C', 'D', 'E', 'F', 'G',
            'H', 'I', 'J', 'K', 'L', 'M', 'N',
            'O', 'P', 'Q', 'R', 'S', 'T',
            'U', 'V', 'W', 'X', 'Y', 'Z',
            '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'
    };

    private static Random random = new Random();

    /**
     * 生成随机文件名：当前年月日时分秒+随机数
     *
     * @return
     */
    public static String getRandomFileName() {

        SimpleDateFormat simpleDateFormat;

        simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");

        Date date = new Date();

        String str = simpleDateFormat.format(date);

        Random random = new Random();

        int rannum = random.nextInt(100000);//

        return str + rannum;// 当前时间
    }

    /**
     * 生成随机字符串
     *
     * @param length
     * @return
     */
    public static String getRandomStr(int length) {
        StringBuffer str = new StringBuffer();
        for (int i = 0; i < length; i++) {
            str.append(chars[random.nextInt(chars.length)]);
        }
        return str.toString();
    }

    public static void main(String[] args) {
        System.out.println(getRandomStr(4));
        System.out.println(getRandomStr(4));
        System.out.println(getRandomStr(16));
    }
}